import random

bank_users = []

# 开户创造user
def creat_account():
    account_num = ''.join(random.choices('0123456789', k=8))
    name = input('请输入您的姓名:')
    while True:
        password1 = input('请输入您的密码(密码为6位数字):')
        if password1.isdigit() and len(password1) == 6:
            password1 = int(password1)
            print('密码输入成功')
            print(f'您的账号为{account_num}')
            break
        else:
            print('您的密码输入无效，必须为六位数字')
            continue
    address = generate_address()
    balance = 0
    bank_name = '中国工商银行昌平支行'
    user_u = {
        "account_num": account_num,
        "name": name,
        "password": password1,
        "address": address,
        "balance": balance,
        "bank_name": bank_name
    }
    return user_u

# 选择地址
def generate_address():
    address_data = {
        '中国':{
            '北京':['长安街','人民路','解放路'],
            '广东':['华南大道','东风路','白云路'],
            '山东': ['泉城路', '文化路', '历下路'],
            '浙江': ['西湖路', '新天地路', '解放路']
        },
        '美国':{
            '纽约':['华尔街','百老汇','林肯大道'],
            '洛杉矶':['好莱坞大道','阳光大道','环球大道']
        },
        '英国':{
            '伦敦':['牛津街','白厅街','国王大道']
        }
    }
    print('请选择您的国家:')
    countries = list(address_data.keys())
    for idx, country in enumerate(countries, 1):
        print(f'{idx}.{country}')
    country_choice = int(input('请选择国家编号:')) - 1
    country = countries[country_choice]
    print(f'请选择您的{country},城市')
    cities = list(address_data[country].keys())
    for idx, city in enumerate(cities, 1):
        print(f'{idx}.{city}')
    city_choice = int(input('请输入您的城市编号:')) - 1
    city = cities[city_choice]
    print(f'请选择您的{country},{city},街道')
    streets = list(address_data[country][city])
    for idx, street in enumerate(streets, 1):
        print(f'{idx}.{street}')
    street_choice = int(input('请输入您的街道编号:')) - 1
    street = streets[street_choice]
    door_number = input('请输入您的门牌号:')
    print(f'您选择的为{country}，{city}，{street}，{door_number}')
    return f"{country}，{city}，{street}，{door_number}"

# 检查user是否存在
def add_user(bank_users, user):
    if len(bank_users) >= 100:
        return 3
    if any(u['account_num'] == user['account_num'] for u in bank_users):
        return 2
    bank_users.append(user)
    return 1

def deposit(bank_users, account_num, amount):
    user = next((u for u in bank_users if u['account_num'] == account_num), None)
    if user:
        user['balance'] += amount
        return True
    return False

def withdraw(bank_users, account_num, password, amount):
    user = next((u for u in bank_users if u['account_num'] == account_num), None)
    if user is None:
        return 1  # 账号不存在
    if user['password'] != password:
        return 2  # 密码不对
    if user['balance'] < amount:
        return 3  # 钱不够
    user['balance'] -= amount
    return 0  # 正常

def transfer(bank_users, from_account, to_account, from_password, amount):
    from_user = next((u for u in bank_users if u['account_num'] == from_account), None)
    to_user = next((u for u in bank_users if u['account_num'] == to_account), None)

    if from_user is None or to_user is None:
        return 1  # 账号不对
    if from_user['password'] != from_password:
        return 2  # 密码不对
    if from_user['balance'] < amount:
        return 3  # 钱不够
    from_user['balance'] -= amount
    to_user['balance'] += amount
    return 0  # 转账成功

def query_account(bank_users, account_num, password):
    user = next((u for u in bank_users if u['account_num'] == account_num), None)
    if user is None:
        print('该用户不存在')
        return
    if user['password'] != password:
        print('密码不正确')
        return
    print(f"当前账号：{user['account_num']}, 余额：{user['balance']}元")
    print(f"用户居住地址：{user['address']}")
    print(f"开户行：{user['bank_name']}")

while True:
    print('*****************************\n'
          '*        中国工商银行         *\n'
          '*        账户管理系统         *\n'
          '*          v 1.0            *\n'
          '*****************************\n'
          '*1.开户                      *\n'
          '*2.存钱                      *\n'
          '*3.取钱                      *\n'
          '*4.转账                      *\n'
          '*5.查询                      *\n'
          '*6.Bye!                     *\n'
          '*****************************\n')

    select_product = int(input('请输入您的业务:').strip())
    if select_product == 1:
        print('您已经选择开户系统')
        user = creat_account()
        result_user = add_user(bank_users, user)
        if result_user == 1:
            print('开户成功')
        elif result_user == 2:
            print('用户已存在')
        else:
            print('用户库已满，请联系管理员操作')
        continue
    if select_product == 2:
        print('您已经选择存钱系统')
        account_num = input('请输入您的账号')
        amount = int(input('请输入您要存入的金额'))
        if deposit(bank_users, account_num, amount):
            print('存款成功')
        else:
            print('用户不存在')
        continue
    if select_product == 3:
        print('您已经选择取钱系统')
        account_num = input('请输入您的账号')
        password = int(input('请输入您的密码'))
        amount = int(input('请输入您要取款的金额'))
        result = withdraw(bank_users, account_num, password, amount)
        if result == 0:
            print("取款成功")
        elif result == 1:
            print("账号不存在")
        elif result == 2:
            print("密码不正确")
        elif result == 3:
            print("余额不足")
        continue
    if select_product == 4:
        print('您已经选择转账系统')
        from_account = input("请输入转出账号: ")
        to_account = input("请输入转入账号: ")
        from_password = int(input("请输入转出账号的密码: "))
        amount = int(input("请输入转账金额: "))
        result = transfer(bank_users, from_account, to_account, from_password, amount)
        if result == 0:
            print("转账成功")
        elif result == 1:
            print("账号不对")
        elif result == 2:
            print("密码不对")
        elif result == 3:
            print("余额不足")
        continue
    if select_product == 5:
        print('您已经选择查询系统')
        account_num = input("请输入账号: ")
        password = int(input("请输入密码: "))
        query_account(bank_users, account_num, password)
        continue
    if select_product == 6:
        print('您已退出本系统，欢迎下次光临')
        break
    print('您的输入有问题')
    continue
